package com.liuym.common.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration
@EnableRedisHttpSession
@EnableCaching(proxyTargetClass = true)
public class RedisConfig extends RedisConfigCommon {
    @Value("${redis.url}")
    private String url;

    @Value("${redis.database}")
    private int database;

    @Value("${redis.password}")
    private String password;

    @Value("${redis.jedis.pool.max-active}")
    private int maxActive;

    @Value("${redis.jedis.pool.max-idle}")
    private int maxIdle;

    @Value("${redis.jedis.pool.min-idle}")
    private int minIdle;

    @Value("${redis.jedis.pool.max-wait}")
    private int maxWait;

    @Value("${redis.cacheName:}")
    private String cacheName;

    @Value("${redis.ttl:60}")
    private int ttl;

    @Primary
    @Bean
    public LettuceConnectionFactory lettuceConnectionFactory() {
        return super.lettuceConnectionFactory(url, password, database);
    }

    // 缓存管理器
    @Primary
    @Bean
    public RedisCacheManager cacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
        return super.cacheManager(lettuceConnectionFactory, ttl, cacheName);
    }

    @Primary
    @Bean
    RedisTemplate<String, String> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        return super.getRedisTemplate(lettuceConnectionFactory);
    }

}
